package com.hwy.catering.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hwy.catering.entity.User;
import com.hwy.catering.interceptor.RootInterceptor;
import com.hwy.catering.service.UserService;
import com.hwy.catering.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


@Api(tags = "后台用户接口")
@RestController
@RequestMapping("/cater/user")
//@CrossOrigin
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "删除用户方法")
    @PostMapping("/deleteUser/{id}")
    public R deleteUser(@PathVariable long id){
        return userService.deleteUser(id);
    }

    @ApiOperation(value = "用户登录方法")
    @PostMapping("/login")
    public R login(@RequestBody User user){
        return userService.login(user);
    }

    @ApiOperation(value = "退出登录方法")
    @PostMapping("/logout")
    public R logout(){
        return userService.logout();
    }

    @ApiOperation(value = "新增用户方法")
    @PostMapping("/addUser")
    public R addUser(@RequestBody User user){
        R result = userService.addUser(user);
        return result;
    }

    @ApiOperation(value = "更改用户方法")
    @PostMapping("/editUser")
    public R editUser(@RequestBody User user){
        R result =userService.editUser(user);
        return result;
    }

    @ApiOperation(value = "分页查询用户")
    @PostMapping("/userPage/{page}/{limit}")
    public R userPage(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) User user){
        Page<User> pageParam = new Page<>(page, limit);
        R r = userService.userPage(pageParam, user);
        return r;
    }

    @ApiOperation(value = "根据ID查用户")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id) {
//        System.out.println(id);
        User user = userService.getById(id);
//        System.out.println(user);
        return R.success(user);
    }

    @ApiOperation(value = "xlsx导入用户")
    @PostMapping("/importUser")
    public R importUser(MultipartFile file){
        R r = userService.importUser(file);
        return r;
    }

    @ApiOperation("xlsx用户导出")
    @GetMapping("/exportUser")
    public void exportUser(HttpServletResponse response) {
        userService.exportUser(response);
    }

    @ApiOperation("更改用户权限")
    @PostMapping("/changeRoot/{id}/{root}")
    public void changeRoot(@PathVariable("id") long id,@PathVariable("root")int root){
        userService.changeRoot(id,root);
    }

    @ApiOperation("更改账号状态")
    @PostMapping("/changeStop/{id}/{stop}")
    public void changeStop(@PathVariable("id") long id,@PathVariable("stop")int stop){
        userService.changeStop(id,stop);
    }

    @ApiOperation("获取最初的root状态")
    @PostMapping("/initRoot/{id}")
    public R initRoot(@PathVariable long id){
        R r = userService.initRoot(id);
        return r;
    }

    @ApiModelProperty("用户详情查询")
    @PostMapping("/detailUser/{id}")
    public R detailUser(@PathVariable long id){
        R r = userService.detailUser(id);
        return r;
    }
}
